
package com.yami.shop.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.dto.NoticeDto;
import com.yami.shop.bean.enums.NoticeType;
import com.yami.shop.bean.model.Notice;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.NoticeMapper;
import com.yami.shop.service.NoticeService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告管理
 *
 * @author hzm
 * @date 2019-04-18 21:21:40
 */
@Service
@AllArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    private NoticeMapper noticeMapper;

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private UserService userService;

    @Override
    @Cacheable(cacheNames = "notices", key = "#shopId")
    public List<Notice> listTopNoticeByShopId(Long shopId) {
        Notice notice = new Notice();
        notice.setShopId(shopId);
        notice.setType(NoticeType.TO_USER.value());
        notice.setStatus(1);
        return noticeMapper.getListByParam(notice);
    }

    @Override
    @CacheEvict(cacheNames = "notices", key = "#shopId")
    public void removeTopNoticeListCacheByShopId(Long shopId) {
    }

    @Override
    public Page<NoticeDto> pageNotice(Page<NoticeDto> page, Notice notice) {
        return noticeMapper.pageNotice(page, notice);
    }

    @Override
    @Cacheable(cacheNames = "notice", key = "#noticeId")
    public Notice getNoticeById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    @CacheEvict(cacheNames = "notice", key = "#noticeId")
    public void removeNoticeCacheById(Long noticeId) {
    }

    @Override
    public void removeByIdAndShopId(Long id, long shopId) {
        noticeMapper.removeByIdAndShopId(id,shopId);
    }

    @Override
    public IPage<Notice> listPage(PageParam<Notice> page, Notice notice) {
        return noticeMapper.listPage(page,notice);
    }

    @Override
    public Notice getInfoById(Long id) {
        Notice notice = noticeMapper.selectById(id);
        if(StringUtils.isNotBlank(notice.getMultiShopIds())){
            List<Long> multiShopIds = Arrays.stream(notice.getMultiShopIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            List<ShopDetail> shopDetailByShopIds = shopDetailService.getShopDetailByShopIds(multiShopIds);
            for (ShopDetail shopDetail : shopDetailByShopIds){
                shopDetail.setMobile(shopDetail.getTel());
            }
            notice.setShopDetailList(shopDetailByShopIds);
        }
        if(StringUtils.isNotBlank(notice.getUserIds())){
            List<String> userIds = Arrays.stream(notice.getUserIds().split(",")).map(s -> s.trim()).collect(Collectors.toList());
            List<User> users = userService.getUserByUserIds(userIds);
            notice.setUserDetailList(users);
        }
        return notice;
    }


}
